#!/bin/bash
#read config file
while read line; do
	eval $line
done < Config/config

if [ -z "$1" ] || [ -z "$2" ]; then
	echo "Required usage is bc10502dms.sh [input file] [instrument]"
	echo "where instrument is either p (primary) or c (collocated)."
	exit
fi

corr_file="$1"
sampler="$2"

tmpfile="bc1050_$temptime"
#build .MIN file
filename_date=$(date +%m_%d_%Y_%H%M%S)
data_file="$saroad-$filename_date.MIN"
touch $data_file
touch $tmpfile
while IFS=, read tstamp eco oco ec oc sflow tflow at bp codes; do
	#echo "$tstamp"
	data_time=`date -d "$tstamp" +%m/%d/%Y\ %H:%M:%S`
			
	ec=${ec//+}
	ec=$(echo $ec | sed 's/^0*//')
	if  [ ! -z "$ec" ]; then
		if [ "$sampler" == "p" ]; then
			echo "$saroad|EC_corr|$data_time|0|$ec|" >> $tmpfile
		fi
		if [ "$sampler" == "c" ]; then
			echo "$saroad|EC_corr_a|$data_time|0|$ec|" >> $tmpfile
		fi
	fi
										
	oc=${oc//+}
	oc=$(echo $oc | sed 's/^0*//')
	if [ ! -z "$oc" ]; then
		if [ "$sampler" == "p" ]; then 
			echo "$saroad|OC_corr|$data_time|0|$oc|" >> $tmpfile
		fi
		if [ "$sampler" == "c" ]; then
			echo "$saroad|OC_corr_a|$data_time|0|$oc|" >> $tmpfile
		fi
	fi
	
	sflow=${sflow//+}
	sflow=$(echo $sflow | sed 's/^0*//')
	if [ ! -z "$sflow" ]; then
		if [ "$sampler" == "p" ]; then 
			echo "$saroad|SAMFL_BC1050|$data_time|0|$sflow|" >> $tmpfile
		fi
		if [ "$sampler" == "c" ]; then
			echo "$saroad|SAMFL_BC1050_a|$data_time|0|$sflow|" >> $tmpfile
		fi
	fi
	
	tflow=${tflow//+}
	tflow=$(echo $tflow | sed 's/^0*//')
	if [ ! -z "$tflow" ]; then
		if [ "$sampler" == "p" ]; then 
			echo "$saroad|TOTFL_BC1050|$data_time|0|$tflow|" >> $tmpfile
		fi
		if [ "$sampler" == "c" ]; then
			echo "$saroad|TOTFL_BC1050_a|$data_time|0|$tflow|" >> $tmpfile
		fi
	fi
	
																	
done < $corr_file
																	
sed 's/$'"/`echo \\\r`/" < $tmpfile > $data_file
												
rm $tmpfile
rm $corr_file